package com.power.sifa.approve.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.domain.YesNo;
import com.plf.common.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.power.sifa.approve.model.Apl_attachment;
import com.power.sifa.approve.model.pageInfo.Apl_attachmentPageInfo;
import com.power.sifa.approve.api.IApl_attachmentFeign;
import com.power.sifa.approve.service.Apl_attachmentService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   申请材料附件控制器
 */

//@Slf4j
//@PowerLog
@RestController
@Api(value = "Apl_attachmentController",tags={"申请材料附件"})
public class Apl_attachmentController implements IApl_attachmentFeign{

    @Resource
    private Apl_attachmentService apl_attachmentService;


    @Override
    @ApiOperation("查询申请材料附件分页列表")
    @SaCheckPermission("apl_attachment_list")
    public ResponseDTO list(Apl_attachmentPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Apl_attachment> page = this.apl_attachmentService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Apl_attachment apl_attachment : page) {
                apl_attachment.geneText();
                list.add(apl_attachment);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看申请材料附件")
    @SaCheckPermission("apl_attachment_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Apl_attachment apl_attachment  = apl_attachmentService.getById(id);
        if (apl_attachment != null) {
            apl_attachment.geneText();
        }
        return ResponseDTO.succData(apl_attachment);
    }

    @Override
    @ApiOperation("删除申请材料附件")
    @SaCheckPermission("apl_attachment_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        apl_attachmentService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改申请材料附件")
    @SaCheckPermission("apl_attachment_save")
    public ResponseDTO save(Apl_attachment apl_attachment) {
        Assert.notNull(apl_attachment, "apl_attachment不能为空");
        apl_attachmentService.saveOrUpdate(apl_attachment);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="批量删除申请材料附件")
    @SaCheckPermission("apl_attachment_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        apl_attachmentService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("apl_attachment_list")
    public void output(HttpServletResponse response, Apl_attachmentPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "申请材料附件";
        String[] heads = {"applyid+attachmentid","附件id","上传时间","引用主表id","引用申请材料id","是否已读"};
        String[] fields = {"id","attachmentid","createtime","applyid","matter_attachid","isread"};
        Page<Apl_attachment> page = this.apl_attachmentService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "申请材料附件";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"applyid+attachmentid","附件id","上传时间","引用主表id","引用申请材料id","是否已读"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","attachmentid","createtime","applyid","matter_attachid","isread"};
        List<Apl_attachment> list = (List<Apl_attachment>)ExcelUtil.Import(fields, file, Apl_attachment.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.apl_attachmentService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

    @Override
    @ApiOperation(value="获取申请材料附件列表")
    public ResponseDTO queryList(String applyid) {
        Assert.notNull(applyid, "applyid不能为空");
        return ResponseDTO.succData(apl_attachmentService.queryList(applyid));
    }

    @Override
    @ApiOperation(value="保存附件已读")
    public ResponseDTO saveRead(String id) {
        Assert.notNull(id, "申请材料附件id不能为空");
        Apl_attachment apl_attachment = apl_attachmentService.getById(id);
        if(apl_attachment != null) {
            apl_attachment.setIsread(YesNo.YES);
            apl_attachmentService.saveOrUpdate(apl_attachment);
        }
        return ResponseDTO.succ();
    }

}
